const {
  createArticle,
  selectArticle,
  articleDetail,
  createCollect,
  isCollect,
  deleteCollect,
  getMyArticle,
  getCollectArticleList,
  hotArticle,
  deleteTheArticle
} = require("../service/article.service");
class Article {
  //发布文章
  async sendArticle(ctx, next) {
    const {
      articleContent,
      articleTitle,
      userId,
      articleType,
      articlePreView,
    } = ctx.request.body;
    await createArticle(
      articleContent,
      articleTitle,
      userId,
      articleType,
      articlePreView
    );
    ctx.body = {
      code: 0,
      message: "文章发布成功",
      data: "",
    };
  }
  //获得所有文章
  async getArticle(ctx, next) {
    let { type, curentPage, pageSize } = JSON.parse(ctx.request.query.data);
    type === -1 ? (type = null) : (type = type);
    const res = await selectArticle(type, curentPage, pageSize);
    ctx.body = {
      code: 0,
      message: "面经查询成功",
      data: res,
    };
  }
  // 获取文章详情
  async getArticleDetail(ctx, next) {
    const { articleId, userId } = JSON.parse(ctx.request.query.data);
    const res = await articleDetail(articleId);
    const collect = (await isCollect(articleId, userId)) ? true : false;
    ctx.body = {
      code: 0,
      message: "面经详情查询成功",
      data: { ...res, collect },
    };
  }
  // 收藏
  async collectArticle(ctx, next) {
    const { articleId, userId } = ctx.request.body;
    try {
      const res = await createCollect(articleId, userId);
      ctx.body = {
        code: 0,
        message: "面经收藏成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "面经收藏失败",
        data: "",
      };
    }
  }
  //取消收藏
  async noCollectArticle(ctx, next) {
    const { articleId, userId } = ctx.request.body;
    try {
      const res = await deleteCollect(articleId, userId);
      ctx.body = {
        code: 0,
        message: "面经取消收藏成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "面经取消收藏失败",
        data: "",
      };
    }
  }
  //查看我的发布
  async myArticle(ctx, next) {
    const { userId, curentPage, pageSize } = ctx.request.body;
    try {
      const res = await getMyArticle(userId, curentPage, pageSize);
      ctx.body = {
        code: 0,
        message: "获取我的发布列表成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "获取我的发布列表失败",
        data: "",
      };
    }
  }
  //查看收藏面经
  async collectArticleList(ctx, next) {
    const { userId, curentPage, pageSize } = ctx.request.body;
    try {
      const res = await getCollectArticleList(userId, curentPage, pageSize);
      ctx.body = {
        code: 0,
        message: "获取收藏面经列表成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "获取收藏面经列表失败",
        data: "",
      };
    }
  }
  //获取热门面经
  async getHotArticle(ctx, next) {
    try {
      const res = await hotArticle();
      ctx.body = {
        code: 0,
        message: "热门面经获取成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "热门面经获取失败",
        data: "",
      };
    }
  }
  //删除面经
  async deleteArticle(ctx, next) {
    const { id } = ctx.request.body
    try {
      const res = await deleteTheArticle(id);
      ctx.body = {
        code: 0,
        message: "删除面经成功",
        data: res,
      };
    } catch (error) {
      ctx.body = {
        code: -1,
        message: "删除面经失败",
        data: "",
      };
    }
  }
}

module.exports = new Article();
